home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Devil's Cubes 1.0.1 / source / Devil’s Cubes ƒ / MSG Shell ƒ / msg graphics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  11.1 KB  |  482 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        msg graphics.c
  4.  
  5. Purpose:    This module handles the about box, about MSG box, main
  6.             window opening/closing/updating, and help windows
  7.             opening/closing/updating.
  8.  
  9.  
  10. Devil’s Cubes -- a simple cubes puzzle
  11. Copyright (C) 1993 Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "msg graphics.h"
  31. #include "msg dialogs.h"
  32. #include "msg error.h"
  33. #include "msg menus.h"
  34. #include "msg sounds.h"
  35. #include "msg environment.h"
  36. #include "msg prefs.h"
  37. #include "cube.h"
  38. #include "cube graphics.h"
  39. #include "cube help.h"
  40. #include "cube load-save.h"
  41.  
  42. Boolean            gInitedWindowBounds;
  43. Rect            gMainScreenBounds;
  44. Rect            gMainWindowBounds;
  45. GDHandle        gBiggestDevice;
  46. WindowPtr        gMainWindow;
  47. WindowPtr        gHelp[NUM_HELP];
  48. int                gHelpWidth;
  49. int                gHelpHeight;
  50. int                gWindowWidth;
  51. int                gWindowHeight;
  52.  
  53. /* internal stuff */
  54. Rect            bRect;
  55. Ptr                myBits;
  56. CGrafPort        myCGrafPort;
  57. CGrafPtr        myCGrafPtr;
  58. CTabHandle        ourCMHandle;
  59. GrafPort        myGrafPort;
  60. GrafPtr            myGrafPtr;
  61.  
  62. void ShowInformation(void)
  63. {
  64.     DialogPtr    theDlog;
  65.     int            itemSelected = 0;
  66.     int            newleft;
  67.     int            newtop;
  68.     int            dlogID;
  69.     
  70.     if(GetWindowDepth() > 2)
  71.         dlogID = colorInfoDialog;
  72.     else
  73.         dlogID = bwInfoDialog;
  74.     
  75.     theDlog = GetNewDialog(dlogID, 0L, (WindowPtr)-1L);
  76.     newleft = gMainScreenBounds.left + (((gMainScreenBounds.right -
  77.                 gMainScreenBounds.left) - (theDlog->portRect.right -
  78.                 theDlog->portRect.left)) / 2);
  79.     newtop = gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  80.                 gMainScreenBounds.top) - (theDlog->portRect.bottom -
  81.                 theDlog->portRect.top)) / 2);
  82.     if(newtop < 15)
  83.         newtop = 15;
  84.     MoveWindow(theDlog, newleft, newtop, TRUE);
  85.     ShowWindow(theDlog);
  86.     
  87.     while(itemSelected == 0)
  88.     {
  89.         ModalDialog(0L, &itemSelected);
  90.     }
  91.     HideWindow(theDlog);
  92.     DisposeDialog(theDlog);
  93. }
  94.  
  95. void ShowSplashScreen(void)
  96. {
  97.     int                dlogID;
  98.     DialogPtr        theDlog;
  99.     int                itemSelected = 0;
  100.     int                newleft;
  101.     int                newtop;
  102.     
  103.     if(GetWindowDepth() > 2)
  104.         dlogID = colorSplashDialog;
  105.     else
  106.         dlogID = bwSplashDialog;
  107.     
  108.     theDlog = GetNewDialog(dlogID, 0L, (WindowPtr)-1L);
  109.     newleft = gMainScreenBounds.left + (((gMainScreenBounds.right -
  110.                 gMainScreenBounds.left) - (theDlog->portRect.right -
  111.                 theDlog->portRect.left)) / 2);
  112.     newtop = gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  113.                 gMainScreenBounds.top) - (theDlog->portRect.bottom -
  114.                 theDlog->portRect.top)) / 2);
  115.     if(newtop < 15)
  116.         newtop = 15;
  117.     MoveWindow(theDlog, newleft, newtop, TRUE);
  118.     
  119.     ParamText(gMyName,gMyOrg,"","");
  120.  
  121.     DoSound(sound_aboutMSG);
  122.     OpenTheSoundDevice();
  123.     ShowWindow(theDlog);
  124.     while(itemSelected == 0)
  125.         ModalDialog(ProcOFilter, &itemSelected);
  126.     
  127.     HideWindow(theDlog);
  128.     DisposeDialog(theDlog);
  129.     CloseTheSoundDevice();
  130. }
  131.  
  132. void OpenMainWindow(void)
  133. {
  134.     if(!gMainWindow)
  135.     {
  136.         if(!gInitedWindowBounds)
  137.         {
  138.             gMainWindowBounds.left = gMainScreenBounds.left + (((gMainScreenBounds.right -
  139.                         gMainScreenBounds.left) - gWindowWidth) / 2);
  140.             gMainWindowBounds.top = 9+ gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  141.                         gMainScreenBounds.top) - gWindowHeight) / 2);
  142.             if(gMainWindowBounds.top < 30)
  143.                 gMainWindowBounds.top = 30;
  144.             gMainWindowBounds.bottom = gMainWindowBounds.top + gWindowHeight;
  145.             gMainWindowBounds.right = gMainWindowBounds.left + gWindowWidth;
  146.             gInitedWindowBounds = 1;
  147.         }
  148.         
  149.         myCGrafPtr=0L;
  150.         myGrafPtr=0L;
  151.  
  152.         if(gHasColorQD)
  153.         {
  154.             gMainWindow = NewCWindow(0L, &gMainWindowBounds, "\pDevil’s Cubes", TRUE,
  155.                     noGrowDocProc, (WindowPtr)-1L, TRUE, 0L);
  156.         }
  157.         else
  158.         {
  159.             gMainWindow = NewWindow(0L, &gMainWindowBounds, "\pDevil’s Cubes", TRUE,
  160.                     noGrowDocProc, (WindowPtr)-1L, TRUE, 0L);
  161.         }
  162.  
  163.         bRect = gMainWindow->portRect;
  164.     }
  165.     
  166.     if(gMainWindow)
  167.     {
  168.         SetPort(gMainWindow);
  169.         InvalRect(&(gMainWindow->portRect));
  170.     }
  171.     else ErrorString("\pThere is not enough memory to open the main window.","\p");
  172. }
  173.  
  174. void GetMainScreenBounds(void)
  175. {
  176.     gMainScreenBounds = screenBits.bounds;
  177.     gMainScreenBounds.top += MBarHeight;
  178. }
  179.  
  180. int GetWindowDepth(void)
  181. {
  182.     Rect        tempRect;
  183.     long        biggestSize;
  184.     long        tempSize;
  185.     GDHandle    thisHandle;
  186.     
  187.     if(gHasColorQD)
  188.     {
  189.         if(gMainWindow)
  190.         {
  191.             thisHandle = GetDeviceList();
  192.             gBiggestDevice = 0L;
  193.             biggestSize = 0L;
  194.             
  195.             while(thisHandle)
  196.             {
  197.                 if(TestDeviceAttribute(thisHandle, screenDevice) &&
  198.                             TestDeviceAttribute(thisHandle, screenActive))
  199.                     if(SectRect(&(gMainWindow->portRect), &((**thisHandle).gdRect),
  200.                                 &tempRect))
  201.                         if(biggestSize < (tempSize =
  202.                                 ((long)(tempRect.bottom - tempRect.top))
  203.                                 * ((long)(tempRect.right - tempRect.left))))
  204.                         {
  205.                             biggestSize = tempSize;
  206.                             gBiggestDevice = thisHandle;
  207.                         }
  208.                 thisHandle = GetNextDevice(thisHandle);
  209.             }
  210.             
  211.             if(gBiggestDevice)
  212.                 return (**(**gBiggestDevice).gdPMap).pixelSize;
  213.             else
  214.                 return 1;
  215.         }
  216.         else
  217.         {
  218.             return (**(**GetMainDevice()).gdPMap).pixelSize;
  219.         }
  220.     }
  221.     else
  222.     {
  223.         return 1;
  224.     }
  225. }
  226.  
  227. void UpdateBoard(void)
  228. {
  229.     long        offRowBytes, sizeOfOff;
  230.     int            theDepth, i, err;
  231.     GDHandle    oldDevice;
  232.     static int    gLastDepth;
  233.  
  234.     if((theDepth = GetWindowDepth()) > 2)
  235.     {
  236.         /* if we just changed from one color depth to another color depth */
  237.         if((myCGrafPtr != 0) && (gLastDepth != theDepth))
  238.         {
  239.             DisposeHandle((**(myCGrafPort).portPixMap).pmTable);
  240.             DisposePtr((**(myCGrafPort).portPixMap).baseAddr);
  241.             CloseCPort(myCGrafPtr);
  242.             myCGrafPtr = 0;
  243.         }
  244.         
  245.         if (myCGrafPtr==0L)
  246.         {
  247.             /* if we just switched from b/w to color, delete the b/w port */
  248.             if(myGrafPtr != 0L)
  249.             {
  250.                 DisposePtr(myGrafPort.portBits.baseAddr);
  251.                 ClosePort(myGrafPtr);
  252.                 myGrafPtr = 0;
  253.             }
  254.             
  255.             if(gBiggestDevice)
  256.             {
  257.                 oldDevice = GetGDevice();
  258.                 SetGDevice(gBiggestDevice);
  259.             }
  260.             else
  261.                 oldDevice = 0;
  262.             
  263.             myCGrafPtr = &myCGrafPort;
  264.             OpenCPort(myCGrafPtr);
  265.             gLastDepth = theDepth = (**(myCGrafPort).portPixMap).pixelSize;
  266.             
  267.             offRowBytes = (((theDepth * (bRect.right - bRect.left)) + 15) >> 4) << 1;
  268.             sizeOfOff = (long)(bRect.bottom - bRect.top) * offRowBytes;
  269.             OffsetRect(&bRect, -bRect.left, -bRect.top);
  270.             
  271.             myBits = NewPtr(sizeOfOff);
  272.             if(myBits == 0L)
  273.                 ErrorString("\pThere is not enough memory to open the main window.","\p");
  274.             
  275.             (**(myCGrafPort).portPixMap).baseAddr = myBits;
  276.             (**(myCGrafPort).portPixMap).rowBytes = offRowBytes + 0x8000;
  277.             (**(myCGrafPort).portPixMap).bounds = bRect;
  278.             
  279.             myCGrafPort.portRect = bRect;
  280.             
  281.             ourCMHandle = (**(**gBiggestDevice).gdPMap).pmTable;
  282.             err = HandToHand(&ourCMHandle);
  283.             if(err != noErr)
  284.                 ErrorString("\pThere is not enough memory to open the main window.","\p");
  285.             
  286.             for(i = 0; i <= (**ourCMHandle).ctSize; i++)
  287.                 (**ourCMHandle).ctTable[i].value = i;
  288.             (**ourCMHandle).ctFlags &= 0x7fff;
  289.             (**ourCMHandle).ctSeed = GetCTSeed();
  290.             
  291.             (**(myCGrafPort).portPixMap).pmTable = ourCMHandle;
  292.             
  293.             if(oldDevice)
  294.                 SetGDevice(oldDevice);
  295.         }
  296.         
  297.         UpdateBoardColor();
  298.     }
  299.     else
  300.     {
  301.         if (myGrafPtr==0L)
  302.         {
  303.             if(myCGrafPtr != 0)
  304.             {
  305.                 DisposeHandle((**(myCGrafPort).portPixMap).pmTable);
  306.                 DisposePtr((**(myCGrafPort).portPixMap).baseAddr);
  307.                 CloseCPort(myCGrafPtr);
  308.                 myCGrafPtr = 0;
  309.             }
  310.             
  311.             myGrafPtr = &myGrafPort;
  312.             OpenPort(myGrafPtr);
  313.             
  314.             offRowBytes = (((bRect.right - bRect.left) + 15) >> 4) << 1;
  315.             sizeOfOff = (long)(bRect.bottom - bRect.top) * offRowBytes;
  316.             OffsetRect(&bRect, -bRect.left, -bRect.top);
  317.             
  318.             myBits = NewPtr(sizeOfOff);
  319.             if(myBits == 0L)
  320.                 ErrorString("\pThere is not enough memory to open the main window.", "\p");
  321.             
  322.             myGrafPort.portBits.baseAddr = myBits;
  323.             myGrafPort.portBits.rowBytes = offRowBytes;
  324.             myGrafPort.portBits.bounds = bRect;
  325.             myGrafPort.portRect = bRect;
  326.         }
  327.         
  328.         UpdateBoardBW();
  329.     }
  330. }
  331.  
  332. void UpdateBoardColor(void)
  333. {
  334.     GDHandle    oldDevice;
  335.     RgnHandle    oldClipRgn;
  336.     
  337.     SetPort(gMainWindow);
  338.     
  339.     oldDevice = GetGDevice();
  340.     SetGDevice(gBiggestDevice);
  341.     
  342.     oldClipRgn = myCGrafPort.clipRgn;
  343.     myCGrafPort.clipRgn = gMainWindow->visRgn;
  344.     
  345.     SetPort((GrafPtr)myCGrafPtr);
  346.     
  347.     DrawBoardColor();
  348.     
  349.     SetPort(gMainWindow);
  350.     SetGDevice(oldDevice);
  351.     
  352.     CopyBits(&(((GrafPtr)myCGrafPtr)->portBits),
  353.                 &(gMainWindow->portBits), &bRect, &bRect, 0, 0L);
  354.     
  355.     myCGrafPort.clipRgn = oldClipRgn;
  356. }
  357.  
  358. void UpdateBoardBW(void)
  359. {
  360.     RgnHandle    oldClipRgn;
  361.     
  362.     SetPort(gMainWindow);
  363.     
  364.     oldClipRgn = myGrafPort.clipRgn;
  365.     myGrafPort.clipRgn = gMainWindow->visRgn;
  366.     
  367.     SetPort(myGrafPtr);
  368.     
  369.     DrawBoardBW();
  370.     
  371.     SetPort(gMainWindow);
  372.     
  373.     CopyBits(&(myGrafPtr->portBits),
  374.                 &(gMainWindow->portBits), &bRect, &bRect, 0, 0L);
  375.     
  376.     myGrafPort.clipRgn = oldClipRgn;
  377. }
  378.  
  379. void CloseMainWindow(void)
  380. {
  381.     gameFile.name[0]=0x00;
  382.     
  383.     DisposeWindow(gMainWindow);
  384.     gMainWindow=0L;
  385.     gInitedWindowBounds=FALSE;
  386.     
  387.     if(myCGrafPtr != 0)
  388.     {
  389.         DisposeHandle((**(myCGrafPort).portPixMap).pmTable);
  390.         DisposePtr((**(myCGrafPort).portPixMap).baseAddr);
  391.         CloseCPort(myCGrafPtr);
  392.         myCGrafPtr = 0;
  393.     }
  394.     if(myGrafPtr != 0L)
  395.     {
  396.         DisposePtr(myGrafPort.portBits.baseAddr);
  397.         ClosePort(myGrafPtr);
  398.         myGrafPtr = 0;
  399.     }
  400.     if ((myCGrafPtr!=0L) || (myGrafPtr!=0L))
  401.         DisposPtr(myBits);
  402.     
  403.     AdjustMenus();
  404.     DrawMenuBar();
  405. }
  406.  
  407. void OpenHelpWindow(int whichHelp)
  408. {
  409.     Rect    helpRect;
  410.     
  411.     if (!(gHelp[whichHelp]))
  412.     {
  413.         helpRect.left=10+20*whichHelp;
  414.         helpRect.top=50+20*whichHelp;
  415.         helpRect.bottom=helpRect.top+gHelpHeight;
  416.         helpRect.right=helpRect.left+gHelpWidth;
  417.         
  418.         gHelp[whichHelp]=NewWindow(0L, &helpRect, "\p", TRUE, noGrowDocProc, 
  419.                                     (WindowPtr)-1L, TRUE, 0L);
  420.         SetHelpTitle(whichHelp);
  421.     }
  422.     
  423.     SetPort(gHelp[whichHelp]);
  424.     InvalRect(&((gHelp[whichHelp])->portRect));
  425. }
  426.  
  427. void SetHelpTitle(int whichHelp)
  428. {
  429.     Str255        name;
  430.     
  431.     GetItem(gHelpMenu, whichHelp + 1, name);
  432.     SetWTitle(gHelp[whichHelp], name);
  433. }
  434.  
  435. void UpdateHelp(int whichHelp)
  436. {
  437.     long        offRowBytes, sizeOfOff;
  438.     Ptr            myBits;
  439.     Rect        bRect;
  440.     GrafPort    myGrafPort;
  441.     GrafPtr        myGrafPtr;
  442.     RgnHandle    oldClipRgn;
  443.     
  444.     SetPort(gHelp[whichHelp]);
  445.     
  446.     bRect = (gHelp[whichHelp])->portRect;
  447.     
  448.     myGrafPtr = &myGrafPort;
  449.     OpenPort(myGrafPtr);
  450.     
  451.     offRowBytes = ((gHelpWidth + 15) >> 4) << 1;
  452.     sizeOfOff = (long)(gHelpHeight) * offRowBytes;
  453.     OffsetRect(&bRect, -bRect.left, -bRect.top);
  454.     
  455.     myBits = NewPtr(sizeOfOff);
  456.     if(myBits == 0L)
  457.         ErrorString("\pThere is not enough memory.  ", "\p");
  458.     
  459.     myGrafPort.portBits.baseAddr = myBits;
  460.     myGrafPort.portBits.rowBytes = offRowBytes;
  461.     myGrafPort.portBits.bounds = bRect;
  462.     
  463.     myGrafPort.portRect = bRect;
  464.     
  465.     oldClipRgn = myGrafPort.clipRgn;
  466.     myGrafPort.clipRgn = (gHelp[whichHelp])->visRgn;
  467.     
  468.     SetPort(myGrafPtr);
  469.     
  470.     DrawHelp(whichHelp);
  471.     
  472.     SetPort(gHelp[whichHelp]);
  473.     
  474.     CopyBits(&(myGrafPtr->portBits),
  475.         &((gHelp[whichHelp])->portBits), &bRect, &bRect, 0, 0L);
  476.     
  477.     myGrafPort.clipRgn = oldClipRgn;
  478.     
  479.     ClosePort(myGrafPtr);
  480.     DisposPtr(myBits);
  481. }
  482.